home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.10 Oct 95 / 11.10 Tips & Tidbits / HasRamDisk / HasRamDisk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-11  |  4.8 KB  |  170 lines  |  [TEXT/MMCC]

  1. /******************************************************************************
  2.      HasRamDisk.c
  3.  
  4.     A dynamic check for the existence of a RAM disk as created by Apple's
  5.     Memory control panel. Since the user can remove the RAM disk at any time
  6.     by turning it off from the Memory control panel, and can rename it at any
  7.     time, the code here needs to be called before assuming the existence of
  8.     a RAM disk. In other words, don't just check once during your application
  9.     startup and assume thereafter that it will still be there, check ideally
  10.     before each access. Program defensively...
  11.     
  12.     history:
  13.  
  14.         modified:    xx/xx/xx        who are you? what did you do?
  15.         created:    08/10/94        greg poole
  16.  
  17.     Greg Poole
  18.     Vital Images, Inc.
  19.     505 N. 4th Street
  20.     Fairfield, IA 52556
  21.     (515) 472-7726
  22.     email: greg@vitalimages.com
  23.  
  24.  ******************************************************************************/
  25.  
  26. #include <string.h>
  27. #include "HasRamDisk.h"
  28.  
  29. // this structure is based on the DRVR definition in MPWTypes.r
  30. //
  31. struct DRVRresourceRec
  32. {
  33.     // description of drvrFlags
  34.     //
  35.     //    struct
  36.     //    {
  37.     //        unsigned hiUnused     : 1;    // unused
  38.     //        unsigned needLock     : 1;    // lock drvr in memory
  39.     //        unsigned needTime     : 1;    // for periodic action
  40.     //        unsigned needGoodbye  : 1;    // call before heap reinit
  41.     //        unsigned statusEnable : 1;    // responds to status
  42.     //        unsigned ctlEnable    : 1;     // responds to control
  43.     //        unsigned writeEnable  : 1;    // responds to write
  44.     //        unsigned readEnable   : 1;    // responds to read
  45.     //        unsigned loUnused     : 8;    // low byte of drvrFlags word unused
  46.     //    } drvrFlags;
  47.     
  48.     short            drvrFlags;            // flags as defined above    
  49.     unsigned short    driverDelay;        // driver delay (ticks)
  50.     short            deskAccEventMask;    // desk acc event mask
  51.     short            driverMenuID;        // driver menu ID
  52.  
  53.     unsigned short     offsetOpen;            // offset to DRVRRuntime open
  54.     unsigned short     offsetPrime;        // offset to DRVRRuntime prime
  55.     unsigned short     offsetControl;        // offset to DRVRRuntime control
  56.     unsigned short     offsetStatus;        // offset to DRVRRuntime status
  57.     unsigned short     offsetClose;        // offset to DRVRRuntime close
  58.  
  59.     Str31            driverName;            // driver name
  60.     char            driverCode[1];         // driver code
  61. };
  62. typedef struct DRVRresourceRec DRVRresourceRec;
  63. typedef DRVRresourceRec *DRVRresourcePtr, **DRVRresourceHndl;
  64.  
  65. // constants
  66. //
  67. const char kDrvrHandleBit = 0x40;        // bit 7 of 'DRVR' dCtlFlags signals driver is handle
  68.                                         // instead of pointer and needs to be locked in memory
  69. const char kRamDiskName[] = "\p.EDisk";    // Apple's RAM disk driver name
  70.  
  71.  
  72. // pass in an FSSpecPtr to hold a reference to a RAM disk,
  73. // returns TRUE if there is currently a RAM disk, FALSE if not
  74. //
  75. Boolean    HasRamDisk( FSSpecPtr ramDiskSpec )
  76. {
  77.     Boolean                hasRamDisk = FALSE, isHandle = FALSE;
  78.     short                 whichVol = 1;        // start with first disk volume
  79.     HVolumeParam        volPB;
  80.     OSErr                theErr = noErr, anErr = noErr;
  81.     DCtlHandle            dctlHndl = NULL;
  82.     DRVRresourcePtr        drvrPtr = NULL;
  83.     DRVRresourceHndl    drvrHndl = NULL;
  84.     Ptr                    aPtr = NULL;
  85.     Str31                volName;
  86.  
  87.     do                                    // test each mounted disk volume
  88.     {
  89.         volPB.ioNamePtr = volName;
  90.         volPB.ioVRefNum = 0;            // 0 means use ioVolIndex
  91.         volPB.ioVolIndex = whichVol;    // use this to determine volume
  92.     
  93.         if ( ( theErr = PBHGetVInfoSync( (HParmBlkPtr) &volPB ) ) == noErr )
  94.         {
  95.             // get this volume's device control entry from the unit table.
  96.             // do not lock the dctlHndl, I spent a couple of days figuring
  97.             // out that locking this handle causes a crash in the CompServer
  98.             // because it is locked at interrupt time...
  99.             //
  100.             if ( ( dctlHndl = GetDCtlEntry( volPB.ioVDRefNum ) ) != NULL )
  101.             {
  102.                 // is the device's driver in a handle or a pointer?
  103.                 //
  104.                 if ( ( isHandle = (*dctlHndl)->dCtlFlags & kDrvrHandleBit ) != 0 )
  105.                 {
  106.                     drvrHndl = (DRVRresourceHndl) (*dctlHndl)->dCtlDriver;
  107.                     drvrPtr = *drvrHndl;
  108.                 }
  109.                 else
  110.                     drvrPtr = (DRVRresourcePtr) (*dctlHndl)->dCtlDriver;
  111.  
  112.                 // get this device's driver, check if it is a RAM disk
  113.                 //
  114.                 if ( !memcmp( drvrPtr->driverName, kRamDiskName, *kRamDiskName + 1 ) )
  115.                 {
  116.                     // this driver is the RAM disk driver, create an FSSpec to its root dir
  117.                     //
  118.                     anErr = FSMakeFSSpec( volPB.ioVRefNum, fsRtDirID, volName, ramDiskSpec );
  119.                     if ( anErr == noErr )
  120.                         hasRamDisk = TRUE;
  121.                     break;
  122.                 }
  123.             }
  124.         }
  125.         whichVol++; // go to next volume
  126.     } 
  127.     while ( theErr != nsvErr );
  128.  
  129.     return hasRamDisk;
  130.     
  131. } // end HasRamDisk
  132.  
  133.  
  134. // define TEST_RAM_DISK for a standalone test
  135. //
  136. #define TEST_RAM_DISK
  137.  
  138. #if defined( TEST_RAM_DISK )
  139.  
  140.     // local function prototypes
  141.     //
  142.     static void InitTheMac( void );
  143.     
  144.     static void InitTheMac( void )
  145.     {
  146.         InitGraf( &qd.thePort );
  147.         InitFonts();
  148.         InitWindows();
  149.         InitMenus();
  150.         TEInit();
  151.         InitDialogs( 0L );
  152.         InitCursor();
  153.         MaxApplZone();
  154.     
  155.     } // end InitTheMac
  156.  
  157.     void main( void )
  158.     {
  159.         FSSpec        ramDiskSpec;        
  160.         Boolean        hasRamDisk;
  161.         
  162.         InitTheMac();
  163.         hasRamDisk = HasRamDisk( &ramDiskSpec );
  164.         
  165.     } // end main
  166.  
  167. #endif // TEST_RAM_DISK
  168.  
  169.  
  170.